package ordinaryArray;

import org.junit.Test;

/*
 *@description Z 字形变换
 *@author hkfan
 *@version 1.0
 *@create 2025/5/27 10:26
 */
public class LeetCode_6 {

    @Test
    public void test(){
        System.out.println(convert("PAYPALISHIRING",4));
    }


    public String convert(String s, int numRows) {
        //0. 排除特殊情况
        if (numRows==1){
            return s;
        }
        //1. 定义
        int num = (numRows-1)*2,i=1,j=1;
        String res = "";
        //2. 循环添加
        do{
            res+=cal(s,num,i,j);
            i=(i+1)%num;
            j=(j-1+num)%num;
        }while (i!=j);
        res+=cal(s,num,i,j);
        return res;
    }

    private String cal(String s, int num, int i,int k) {
        String res = "";
        for (int j = 0; j < s.length(); j++) {
            if ((j+1)%num==i || (j+1)%num==k){
                res += s.charAt(j);
            }
        }
        return res;
    }

}
